package com.jokins.admin.ios.user.action;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jokins.admin.ios.user.actioncontext.BookUserSignInDoc;
import com.jokins.admin.ios.user.mapper.BookUserMapper;
import com.jokins.admin.ios.user.mapper.BookUserSignInMapper;
import com.jokins.admin.ios.user.param.BookUserSignInParam;
import com.jokins.common.actions.action.ActionContext;
import com.jokins.common.actions.action.BaseAction;
import com.jokins.common.actions.exception.BusinessException;
import com.jokins.common.constant.LevelValueEnum;
import com.jokins.common.entry.ios.user.BookUser;
import com.jokins.common.entry.ios.user.BookUserSignIn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 〈〉<br>
 *
 * @author Mr.Wang
 * @create 2025/2/13
 */
@Slf4j
@Component
public class BookUserSignInDocAction extends BaseAction<BookUserSignInDoc> {

    @Autowired
    private BookUserMapper bookUserMapper;

    @Autowired
    private BookUserSignInMapper bookUserSignInMapper;

    @Override
    protected void process(BookUserSignInDoc actionContext) throws BusinessException {
        BookUserSignInParam param = actionContext.getParam();
        Map<String,Object> reMap=new HashMap<>();
        String userId = param.getUserId();
        int experience = param.getExperience();

        // 查询用户信息
        BookUser user = bookUserMapper.selectById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 查询用户上次签到记录
        LambdaQueryWrapper<BookUserSignIn> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(BookUserSignIn::getUserId,userId)
                .orderByDesc(BookUserSignIn::getCreatedTime)
                .last("limit  1");
        BookUserSignIn lastSignIn = bookUserSignInMapper.selectOne(queryWrapper);
        int totalExperience = lastSignIn != null ? lastSignIn.getTotalExperience() : 0;
        totalExperience += experience;

        // 插入本次签到记录
        BookUserSignIn newSignIn = new BookUserSignIn();
        newSignIn.setUserId(userId);
        newSignIn.setSignInDate(new Date());
        newSignIn.setExperience(experience);
        newSignIn.setTotalExperience(totalExperience);
        bookUserSignInMapper.insert(newSignIn);

        // 更新用户等级
        updateUserLevel(user, totalExperience,experience,reMap);
        actionContext.setResult(ActionContext.RESULT_DOC_KEY,reMap);
    }

    private void updateUserLevel(BookUser user, int totalExperience,int experience,Map<String,Object> reMap) {
        LevelValueEnum currentLevelEnum = LevelValueEnum.getByLevel(user.getLevel());
        if (currentLevelEnum == null) {
            throw new RuntimeException("未找到当前等级对应的枚举信息");
        }

        // 检查是否可以升级到下一个等级
        boolean upgrade= LevelValueEnum.canUpgrade(totalExperience,user.getLevel());
        reMap.put("level",user.getLevel());
        reMap.put("levelValueEnd",user.getLevelValue());
        reMap.put("isLevel",false);
        if(upgrade) {
            // 更新用户的等级和等级数值
            Map<String,Object> leveInfoMap=  LevelValueEnum.getNextLevel(user.getLevel());
            String level=leveInfoMap.get("level").toString();
            int levelValueEnd=(int)leveInfoMap.get("levelValueEnd");
            user.setLevel(level);
            user.setLevelValue(levelValueEnd);
            bookUserMapper.updateById(user);
            reMap.put("level",level);
            reMap.put("levelValueEnd",levelValueEnd);
            reMap.put("isLevel",true);
        }
        reMap.put("totalExperience",totalExperience);
        reMap.put("experience",experience);

    }
}